const LogModel = require('../../model/Log');
const moment = require('moment')
const Recommendation = require('../../model/Recommendation')
const Product = require('../../model/Product')
const Store = require('../../model/Store')
const Activity = require('../../model/Activity')
const Dish = require('../../model/Dish')
const Voucher = require('../../model/Voucher')
const async = require('async')

module.exports = {

    //获取日志列表
    changeRecommendation: async(req, res, next) => {
        try {
            let recommendation = await Recommendation.findOne({ page: req.body.page, isDelete: 0 }).exec()
            if (recommendation == null) {
                let newRecommendation = new Recommendation({
                    page: req.body.page,
                    updatedAt: new Date(),
                    detail: req.body.detail,
                    isDelete: 0
                })
                await newRecommendation.save()
                return res.ok('创建推荐成功', newRecommendation)
            } else {
                recommendation.page = req.body.page,
                    recommendation.updatedAt = new Date(),
                    recommendation.detail = req.body.detail,
                    await recommendation.save()
                return res.ok('修改推荐成功', recommendation)
            }




            // if (req.body.id) {
            //     let recommendation = await Recommendation.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            //         // if (req.body.product) {
            //         //     recommendation.product = req.body.product
            //         // }
            //         // if (req.body.store) {
            //         //     recommendation.product = req.body.store
            //         // }
            //         // recommendation.type = req.body.type,
            //     recommendation.page = req.body.page,
            //         // recommendation.title = req.body.title,
            //         recommendation.updatedAt = new Date(),
            //         // recommendation.subtitle = req.body.subtitle,
            //         // recommendation.images = req.body.images,
            //         recommendation.detail = req.body.detail,
            //         // recommendation.refLink = req.body.refLink
            //         await recommendation.save()
            //     return res.ok('修改推荐成功', recommendation)
            // } else if (!req.body.id) {
            //     // let product = undefined
            //     // let store = undefined
            //     // if (req.body.product) {
            //     //     product = req.body.product
            //     // }
            //     // if (req.body.store) {
            //     //     store = req.body.store
            //     // }
            //     let newRecommendation = new Recommendation({
            //         // product: product,
            //         // store: store,
            //         // type: req.body.type,
            //         page: req.body.page,
            //         // title: req.body.title,
            //         updatedAt: new Date(),
            //         // subtitle: req.body.subtitle,
            //         // images: req.body.images,
            //         detail: req.body.detail,
            //         isDelete: 0,
            //         // refLink: req.body.refLink,
            //     })
            //     await newRecommendation.save()
            //     return res.ok('创建推荐成功', newRecommendation)
            // }
        } catch (e) {
            next(e)
        }
    },


    getRecommendationList: async(req, res, next) => {
        try {
            let option = {}
            option.isDelete = 0
            if (req.body.title) {
                option.title = new RegExp(req.body.title, 'i')
            }
            if (req.body.pageType) {
                option.page = { $in: req.body.pageType }
            }
            if (req.body.isDelete) {
                option.isDelete = 0
            }
            let count = await Recommendation.countDocuments(option).exec()
            let page = req.body.page || 1
            let pageSize = req.body.pageSize || count
            let data = await Recommendation.find(option).skip((page - 1) * pageSize).limit(pageSize).exec()
            let result = {}
            result.total = count
            result.currentPage = page
            result.data = data
            result.pageSize = pageSize
            return res.ok('获取列表成功', result)
        } catch (e) {
            next(e)
        }
    },

    deleteRecommendation: async(req, res, next) => {
        try {
            let recommendation = await Recommendation.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            recommendation.isDelete = 1
            await recommendation.save()
            return res.ok('删除推荐成功')
        } catch (e) {
            next(e)
        }
    },


    getOneRecommendation: async(req, res, next) => {
        try {
            let option = {}
            option.isDelete = 0
            if (req.body.id) {
                option._id = req.body.id
            }
            if (req.body.page) {
                option.page = req.body.page
            }
            let recommendation = await Recommendation.findOne(option).populate('product store', 'name').exec()
            console.log(recommendation.product)
            return res.ok('查询活动成功', recommendation)
        } catch (e) {
            next(e)
        }
    },
    addRecommendationPage: async(req, res, next) => {
        try {
            async.each(req.body.id, async function(item, callback) {
                let recommendation = await Recommendation.findOne({ _id: item, isDelete: 0 }).exec()
                let index = recommendation.page.indexOf(req.body.page)
                if (index == -1) {
                    recommendation.page.push(req.body.page)
                    recommendation.markModified('page')
                    await recommendation.save()
                }
            })
            return res.ok('修改成功')
        } catch (e) {
            next(e)
        }
    },
    deleteRecommendationPage: async(req, res, next) => {
        try {
            let recommendation = await Recommendation.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            let index = recommendation.page.indexOf(req.body.page)
            if (index != -1) {
                recommendation.page.splice(index, 1)
                recommendation.markModified('page')
                await recommendation.save()
                return res.ok('修改成功', recommendation)
            } else {
                return res.ok('不存在', )
            }
        } catch (e) {
            next(e)
        }
    },
    addRecommendationItem: async(req, res, next) => {
        try {
            let recommendation = await Recommendation.findOne({ page: req.body.page, isDelete: 0 }).exec()
            recommendation.detail.push(req.body.detail)
            recommendation.markModified('detail')
            await recommendation.save()
            return res.ok('添加成功')
        } catch (e) {
            next(e)
        }
    },
    deleteRecommendationItem: async(req, res, next) => {
        try {
            console.log(req.body)
            let recommendation = await Recommendation.findOne({ page: req.body.page, isDelete: 0 }).exec()
                // if (req.body.detail.type == "product") {
            let index = (recommendation.detail.map(e => { return e.product || e.store || e.activity || e.dish })).indexOf(req.body.detail.product || req.body.detail.store || req.body.detail.activity || req.body.detail.dish)
            recommendation.detail.splice(index, 1)
            recommendation.markModified('detail')
                // } else if (req.body.detail.type == "store") {
                //     let index = (recommendation.detail.map(e => { return e.store || e.product })).indexOf(req.body.detail.store)
                //     recommendation.detail.splice(index, 1)
                //     recommendation.markModified('detail')
                // }
            await recommendation.save()
            return res.ok('删除成功')
        } catch (e) {
            next(e)
        }
    },
    getPageRecommendation: async(req, res, next) => {
        try {
            let recommendation = await Recommendation.findOne({ page: req.body.page, isDelete: 0 }).populate({
                path: 'detail.product',
                model: Product
            }).populate({
                path: 'detail.store',
                model: Store
            }).populate({
                path: 'detail.dish',
                model: Dish
            }).populate({
                path: 'detail.activity',
                model: Activity
            }).populate({
                path: 'detail.voucher',
                model: Voucher,
                populate: { path: 'voucher' }
            }).exec()
            return res.ok('查询成功', recommendation)
        } catch (e) {
            next(e)
        }
    },
    getHomePageRecommendationStore: async(req, res, next) => {
        try {
            let recommendation = await Recommendation.findOne({ page: "homePageRecommendationStore", isDelete: 0 }).populate({
                path: 'detail.store',
                model: Store
            }).exec()
            return res.ok('查询成功', recommendation)
        } catch (e) {
            next(e)
        }
    },

    addHomePageRecommendationStore: async(req, res, next) => {
        try {
            let recommendation = await Recommendation.findOne({ page: "homePageRecommendationStore", isDelete: 0 }).exec()
            if (recommendation != null) {
                storeIdList = recommendation.detail.map(e => { return e.store })
                async.each(req.body.store, async function(item, callback) {
                    let index = storeIdList.indexOf(item)
                    if (index == -1) {
                        recommendation.detail.push({ type: "store", store: item })
                        recommendation.markModified('detail')
                        await recommendation.save()
                    }
                })
                return res.ok('添加成功', recommendation)
            } else {
                let temp = []
                req.body.store.map(e => { temp.push({ type: 'store', store: e }) })
                let newRecommendation = new Recommendation({
                    page: "homePageRecommendationStore",
                    updatedAt: new Date(),
                    detail: temp,
                    isDelete: 0,
                })
                await newRecommendation.save()
                return res.ok('添加成功', newRecommendation)
            }
        } catch (e) {
            next(e)
        }
    },

    deleteHomePageRecommendationStore: async(req, res, next) => {
        try {
            let recommendation = await Recommendation.findOne({ page: "homePageRecommendationStore", isDelete: 0 }).exec()
            if (recommendation != null) {
                storeIdList = recommendation.detail.map(e => { return e.store })
                let index = storeIdList.indexOf(req.body.store)
                if (index != -1) {
                    recommendation.detail.splice(index, 1)
                    recommendation.markModified('detail')
                    await recommendation.save()
                }
            }
            return res.ok('删除成功', recommendation)
        } catch (e) {
            next(e)
        }
    },
    sortHomePageRecommendationStore: async(req, res, next) => {
        try {
            let recommendation = await Recommendation.findOne({ page: "homePageRecommendationStore", isDelete: 0 }).exec()
            if (recommendation != null) {
                let temp = []
                req.body.store.map(e => { temp.push({ type: 'store', store: e }) })
                recommendation.detail = temp
                await recommendation.save()
                return res.ok('排序成功', recommendation)
            }
        } catch (e) {
            next(e)
        }
    },
};